package com.lyf.design.learn.j2e.interceptorfilter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LYF
 * @date 2022/6/27
 */
public class FilterChain {
    List<Filter> filterList = new ArrayList<>();
    Target target;

    public void setTarget(Target target) {
        this.target = target;
    }

    void addFilter(Filter filter){
        filterList.add(filter);
    }

    void exec(String req) {
        filterList.forEach(filter ->
                {
                    try {
                        filter.preHandle(req);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        );

        target.exec(req);

        filterList.forEach(filter ->
                filter.postHandle(req));
    }

}
